package com.cooper.java.scraper;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: cooperlee
 * Date: 11/16/12
 * Time: 2:49 PM
 */
public class ResultUrl {

    private static final String URL_FUNCTION_ATTRIBUTE_NAME = "function";
    private static final String URL_ENDODE_ATTRIBUTE_NAME = "encode";
    private static final String URL_TAG = "url";


    public static List<ResultUrl> urlFactory(String axml, String encodingType) {
        String xml = "<root>" + axml + "</root>";
        List<ResultUrl> result = new ArrayList<ResultUrl>();

        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(new ByteArrayInputStream(xml.getBytes()));
        } catch (DocumentException e) {
            e.printStackTrace();
            return result;

        }
        Element root = document.getRootElement();

        for (java.util.Iterator i = root.elementIterator(URL_TAG); i.hasNext(); ) {
            Element foo = (Element) i.next();

            if ((encodingType == null) || "".equals(encodingType)) {
                encodingType = foo.attribute(URL_ENDODE_ATTRIBUTE_NAME) == null ? "" : foo.attribute(URL_ENDODE_ATTRIBUTE_NAME).getValue();
            }
            result.add(new ResultUrl(foo.getText(), foo.attribute(URL_FUNCTION_ATTRIBUTE_NAME) == null ? "" : foo.attribute(URL_FUNCTION_ATTRIBUTE_NAME).getValue(), encodingType));
        }

        /*
   XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
   factory.setNamespaceAware(true);
   XmlPullParser xpp = factory.newPullParser();
   xpp.setInput(new StringReader(xml));
   int eventType = xpp.getEventType();
   boolean inUrlTag = false;
   String url = null;
   String functionTagName = "";
   while (eventType != XmlPullParser.END_DOCUMENT) {
       if (eventType == XmlPullParser.START_TAG) {
           if (URL_TAG.equals(xpp.getName())) {
               inUrlTag = true;
               functionTagName = xpp.getAttributeValue(null,
                       URL_FUNCTION_ATTRIBUTE_NAME);
               if ((encodingType == null) || "".equals(encodingType)) {
                   encodingType = xpp.getAttributeValue(null,
                           URL_ENDODE_ATTRIBUTE_NAME);
               }
           }
       } else if (eventType == XmlPullParser.TEXT) {
           if (inUrlTag) {
               url = xpp.getText();
           }
       } else if (eventType == XmlPullParser.END_TAG) {
           if (URL_TAG.equals(xpp.getName())) {
               inUrlTag = false;
               result.add(new ResultUrl(url, functionTagName, encodingType));
           }
       }
       eventType = xpp.next();
   }     */
        return result;
    }


    public ResultUrl() {
        super();
    }

    public ResultUrl(String url, String function, String encodingType) {
        super();
        this.url = url;
        this.function = function;
        this.encodingType = encodingType;
    }

    private String url;
    private String function;
    private String encodingType;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getFunction() {
        return function;
    }

    public void setFunction(String function) {
        this.function = function;
    }

    public String getEncodingType() {
        return encodingType;
    }

    public void setEncodingType(String encodingType) {
        this.encodingType = encodingType;
    }

}
